#import <Foundation/NSAutoreleasePool.h>
#import <stdlib.h>
#import <stdio.h>
#import <Ice/Ice.h>
#import "event.h"

@implementation Event

-(id) initWithEvent:(Event*)evt
{
	if (![super init])
		return nil;
	
	event = [evt->event mutableCopy];
	comm = evt->comm;
	
	return self;
}

-(id) initWithCommunicator:(id<ICECommunicator>)communicator
{
	if (![super init])
		return nil;
	
	event = [[CollaborationMutableEvent alloc] init];
	
	comm = communicator;
	
	[self setMember:@"type" value:[self fromInt:DEFAULT_EVENT_TYPE]];
	
	return self;
}

-(id) initWithInternalEvent:(CollaborationEvent*)evento andCommunicator:(id)communicator
{
	if (![super init])
		return nil;
	
	comm = communicator;
	
	event = [evento mutableCopy];
	
	return self;
}

-(void) dealloc
{
	[event removeAllObjects];
	[event release];
	[super dealloc];
}

-(BOOL) existsMember:(NSString*)name
{
	if ([event valueForKey:name] != nil)
	{
		return YES;
	}
	else return NO;
}

-(CollaborationEventData*) memberValue:(NSString*)name
{
	return [event valueForKey:name];
	
}

-(void) setMember:(NSString*)name value:(CollaborationEventData*)value
{
	[event setValue:value forKey:name];
}

-(NSUInteger) countMembers
{
	return [event count];
}

-(int) type
{
	CollaborationEventData* ev = [self memberValue:@"type"];
	
	return [self interpretInt:ev];
}

-(CollaborationMutableEvent*) internalEvent
{
	return event;
}

-(BOOL) interpretBool:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [[ICEUtil createInputStream:comm data:ev] retain];
	BOOL b = [os readBool];
	
	[os release];
	
	return b;
}

-(unsigned char) interpretByte:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [[ICEUtil createInputStream:comm data:ev] retain];
	ICEByte b = [os readByte];
	
	[os release];
	
	return b;
}

-(short) interpretShort:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [[ICEUtil createInputStream:comm data:ev] retain];
	short b = [os readShort];
	
	[os release];
	
	return b;
}

-(int) interpretInt:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [[ICEUtil createInputStream:comm data:ev] retain];
	int b = [os readInt];
	
	[os release];
	
	return b;
}

-(long) interpretLong:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [[ICEUtil createInputStream:comm data:ev] retain];
	long b = [os readLong];
	
	[os release];
	
	return b;
}

-(float) interpretFloat:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [[ICEUtil createInputStream:comm data:ev] retain];
	float b = [os readFloat];
	
	[os release];
	
	return b;
}

-(double) interpretDouble:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [[ICEUtil createInputStream:comm data:ev] retain];
	double b = [os readDouble];
	
	[os release];
	
	return b;
}

-(NSString*) interpretString:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [[ICEUtil createInputStream:comm data:ev] retain];
	NSString* b = [os readString];
	
	[os release];
	
	return b;
}

/*-(NSArray*) interpretBoolSeq:(CollaborationEventData*)ev
{
	NSMutableArray* b = [[NSMutableArray alloc] init];
	
	
	
	return b;
}

-(NSArray*) interpretByteSeq:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [ICEUtil createInputStream:comm data:ev];
	NSArray* b = [os readByteSeq];
	//[os release];
	
	return b;
}

-(NSArray*) interpretShortSeq:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [ICEUtil createInputStream:comm data:ev];
	NSArray* b = [os readShortSeq];
	//[os release];
	
	return b;
}

-(NSArray*) interpretIntSeq:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [ICEUtil createInputStream:comm data:ev];
	NSArray* b = [os readIntSeq];
	//[os release];
	
	return b;
}

-(NSArray*) interpretLongSeq:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [ICEUtil createInputStream:comm data:ev];
	NSArray* b = [os readLongSeq];
	//[os release];
	
	return b;
}

-(NSArray*) interpretFloatSeq:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [ICEUtil createInputStream:comm data:ev];
	NSArray* b = [os readFloatSeq];
	//[os release];
	
	return b;
}

-(NSArray*) interpretDoubleSeq:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [ICEUtil createInputStream:comm data:ev];
	NSArray* b = [os readDoubleSeq];
	//[os release];
	
	return b;
}*/

-(NSArray*) interpretStringSeq:(CollaborationEventData*)ev
{
	id<ICEInputStream> os = [[ICEUtil createInputStream:comm data:ev] retain];
	NSArray* b = [os readStringSeq];
	
	[os release];
	
	return b;
}

-(CollaborationEventData*) fromBool:(BOOL)b
{
	CollaborationEventData* ev;
	
	id<ICEOutputStream> os = [[ICEUtil createOutputStream:comm] retain];
	[os writeBool:b];
	ev = [os finished];
	
	[os release];
	
	return ev;
}

-(CollaborationEventData*) fromByte:(unsigned char)b
{
	CollaborationEventData* ev;
	
	id<ICEOutputStream> os = [[ICEUtil createOutputStream:comm] retain];
	[os writeByte:b];
	ev = [os finished];
	
	[os release];
	
	return ev;
}

-(CollaborationEventData*) fromShort:(short)b
{
	CollaborationEventData* ev;
	
	id<ICEOutputStream> os = [[ICEUtil createOutputStream:comm] retain];
	[os writeShort:b];
	ev = [os finished];
	
	[os release];
	
	return ev;
}

-(CollaborationEventData*) fromInt:(int)b
{
	CollaborationEventData* ev;
	
	id<ICEOutputStream> os = [[ICEUtil createOutputStream:comm] retain];
	[os writeInt:b];
	ev = [os finished];
	
	[os release];
	
	return ev;
}

-(CollaborationEventData*) fromLong:(long)b
{
	CollaborationEventData* ev;
	
	id<ICEOutputStream> os = [[ICEUtil createOutputStream:comm] retain];
	[os writeLong:b];
	ev = [os finished];
	
	[os release];
	
	return ev;
}

-(CollaborationEventData*) fromFloat:(float)b
{
	CollaborationEventData* ev;
	
	id<ICEOutputStream> os = [[ICEUtil createOutputStream:comm] retain];
	[os writeFloat:b];
	ev = [os finished];
	
	[os release];
	
	return ev;
}

-(CollaborationEventData*) fromDouble:(double)b
{
	CollaborationEventData* ev;
	
	id<ICEOutputStream> os = [[ICEUtil createOutputStream:comm] retain];
	[os writeDouble:b];
	ev = [os finished];
	
	[os release];
	
	return ev;
}

-(CollaborationEventData*) fromString:(NSString*)b
{
	CollaborationEventData* ev;
	
	id<ICEOutputStream> os = [[ICEUtil createOutputStream:comm] retain];
	[os writeString:b];
	ev = [os finished];
	
	[os release];
	
	return ev;
}

/*-(CollaborationEventData*) fromBoolSeq:(NSArray*)b
{
	CollaborationMutableEventData* ev = [[NSMutableData alloc] init];
	
	for(NSNumber* ns in b)
	{
		[ev appendData:[self fromBool:[ns boolValue]]];
	}
	
	return ev;
}

-(CollaborationEventData*) fromByteSeq:(NSArray*)b
{
	CollaborationMutableEventData* ev = [[NSMutableData alloc] init];
	
	for(NSNumber* ns in b)
	{
		[ev appendData:[self fromByte:[ns unsignedCharValue]]];
	}
	
	return ev;
}

-(CollaborationEventData*) fromShortSeq:(NSArray*)b
{
	CollaborationMutableEventData* ev = [[NSMutableData alloc] init];
	
	for(NSNumber* ns in b)
	{
		[ev appendData:[self fromShort:[ns shortValue]]];
	}
	
	return ev;
}

-(CollaborationEventData*) fromIntSeq:(NSArray*)b
{
	CollaborationMutableEventData* ev = [[NSMutableData alloc] init];
	
	for(NSNumber* ns in b)
	{
		[ev appendData:[self fromInt:[ns intValue]]];
	}
	
	return ev;
}

-(CollaborationEventData*) fromLongSeq:(NSArray*)b
{
	CollaborationMutableEventData* ev = [[NSMutableData alloc] init];
	
	for(NSNumber* ns in b)
	{
		[ev appendData:[self fromLong:[ns longValue]]];
	}
	
	return ev;
}

-(CollaborationEventData*) fromFloatSeq:(NSArray*)b
{
	CollaborationMutableEventData* ev = [[NSMutableData alloc] init];
	
	for(NSNumber* ns in b)
	{
		[ev appendData:[self fromFloat:[ns floatValue]]];
	}
	
	return ev;
}

-(CollaborationEventData*) fromDoubleSeq:(NSArray*)b
{
	CollaborationMutableEventData* ev = [[NSMutableData alloc] init];
	
	for(NSNumber* ns in b)
	{
		[ev appendData:[self fromDouble:[ns doubleValue]]];
	}
	
	return ev;
}*/

-(CollaborationEventData*) fromStringSeq:(NSArray*)b
{
	CollaborationEventData* ev;
	
	id<ICEOutputStream> os = [[ICEUtil createOutputStream:comm] retain];
	[os writeStringSeq:b];
	ev = [os finished];
	
	[os release];
	
	return ev;
}

@end
